package com.roamer.school.web.model;

import com.roamer.school.common.exception.enums.ResultCode;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 统一数据格式
 *
 * @author roamer
 * @version V1.0
 * @date 2019/1/20 19:21
 */
@Data
@NoArgsConstructor
public class ResultBean<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /** 状态码 */
    private Integer code;

    /** 消息 */
    private String message;

    /** 数据 */
    private T data;

    public ResultBean(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 成功
     *
     * @return {@code ResultBean}
     */
    public static ResultBean<?> success() {
        ResultBean result = new ResultBean();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    /**
     * 成功
     *
     * @param data 需要返回的数据
     * @param <E>  数据类型
     *
     * @return {@code ResultBean<E>}
     */
    public static <E> ResultBean<E> success(E data) {
        ResultBean<E> result = new ResultBean<>();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    /**
     * 失败
     *
     * @param message 消息提示
     *
     * @return {@code ResultBean<?>}
     */
    public static ResultBean<?> failure(String message) {
        ResultBean result = new ResultBean<>();
        result.setResultCode(ResultCode.SYSTEM_INNER_ERROR);
        result.setMessage(message);
        return result;
    }

    /**
     * 失败
     *
     * @param resultCode 统一状态码枚举
     *
     * @return {@code ResultBean}
     */
    public static ResultBean<?> failure(ResultCode resultCode) {
        ResultBean result = new ResultBean<>();
        result.setResultCode(resultCode);
        return result;
    }

    /**
     * 失败
     *
     * @param data 需要返回的数据
     * @param <E>  数据类型
     *
     * @return {@code ResultBean<E>}
     */
    public static <E> ResultBean<E> failure(ResultCode resultCode, E data) {
        ResultBean<E> result = new ResultBean<>();
        result.setResultCode(resultCode);
        result.setData(data);
        return result;
    }

    public void setResultCode(ResultCode code) {
        this.code = code.getCode();
        this.message = code.getMessage();
    }

}
